package com.smugmugsoftware.airconcalc.advanced;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class LocaleDialog extends DialogFragment {

	private MainActivityInterface activity;

	private Context context;
	private DialogFragment thisDialog;

	private String[] searchString;

	public LocaleDialog() {}

	public LocaleDialog(Context context, String[] searchString) {
		this.context = context;
		this.thisDialog = this;

		if (searchString == null) {
			this.searchString = new String[] { "" };
		} else {
			this.searchString = searchString;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.activity = (MainActivityInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);

		final TableParser parser = new TableParser(context, R.raw.locales);
		final String[] resultString = parser.getSubIndex(searchString);

		builder.setItems(resultString, new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				String[] newSearchString = new String[searchString.length + 1];
				for (int i = 0; i < searchString.length; i++) {
					newSearchString[i] = searchString[i];
				}
				newSearchString[newSearchString.length - 1] = resultString[which];

				if (searchString.length <= 2) {
					thisDialog.dismiss();
					activity.displayLocaleDialog(context, newSearchString);

				} else {	
					thisDialog.dismiss();
					
					String[] returnString = parser.getLocaleData(newSearchString);
					double[] returnData = new double[returnString.length];
					
					for (int i = 1; i < 10; i++) {
						returnData[i - 1] = zeroCheck(returnString[i]);
					}
					
					activity.getDataFromDialog(NewRoomFragmentInterface.LOCALE_DIALOG, returnData, returnString);
				}
			}
		});
		return builder.create();
	}
	
	protected double zeroCheck(String s) {
		double returnDouble = 0;
		if (s == null || s.length() == 0) {
			return 0;
		} else {
			try {
				returnDouble = Double.parseDouble(s);
			} catch (Exception ex) {
				ex.printStackTrace();
				return 0;
			}
			return returnDouble;
		}
	}
}
